Ccay.Module.define("Ccay.example.ui.igird.rowRenderFunction", function(page, $S) {
	page.ready = function () {
		var gridOption1 = {
			height: 300,
			editMode: true,
			rowRender: "rowSpan",
	        columns: [
				 {columnType: "select", multi: true},
 			     {field: 'name', width:100, header: "name", rowSpan: true, rule: {required: true}},
 				 {field: 'description', width:200, header: "description"},
 				 {field: 'id', width:100, header: "id", editable: false}
	        ],
		    ajax: {
		    	url: 'services/ccay/demo/grid/list/page/${pageSize}/${curPage}/${orderBy}'
		    },
		    operation: {
		    	search: {
		    		btn: '#btnUniteSearch',
					panel: '#rowUniteForm'
		    	},
		    	del: {
		    		btn: '#btnDel1',
		    		undoBtn: '#btnUndoDel1'
		    	},
		    	insert: {
		    		btn: '#btnAdd1',
		    		copy: true,
		    		clear: ['id']
		    	},
		    	save: {
		    		btn: '#btnSave1',
		    		ajax: {
		    			url: "services/ccay/demo/grid/batch"
		    		}
		    	}
		    }
	    };
		
	    var gridOption2 = {
			height: 300,
			editMode: true,
			rowRender: "group",
	        columns: [
				 {columnType: "select", multi: true},
 			     {field: 'name', width:100, header: "name", group: true, rule: {required: true}},
 				 {field: 'description', width:200, header: "description"},
 				 {field: 'id', width: 100, header: 'id', editable: false}
	        ],
		    ajax: {
		    	url: 'services/ccay/demo/grid/list/page/${pageSize}/${curPage}/${orderBy}'
		    },
		    operation: {
		    	search: {
		    		btn: '#btnGroupSearch',
					panel: '#rowGroupForm'
		    	},
		    	del: {
		    		btn: '#btnGroupDel',
		    		undoBtn: '#btnGroupUndoDel'
		    	},
		    	insert: {
		    		btn: '#btnAdd2',
		    		copy: true,
		    		clear: ['id']
		    	},
		    	save: {
		    		btn: '#btnGroupSave',
		    		ajax: {
		    			url: "services/ccay/demo/grid/batch"
		    		}
		    	}
		    }
	    };
		
		var datas3 = [
			{id: 1, name: '1', age:22, pid: null},
			{id: 2, name: '1-1', age:22, pid: 1},
			{id: 12, name: '1-1-1', age:22, pid: 2},
			{id: 13, name: '1-1-2', age:22, pid: 2},
			{id: 14, name: '1-1-2-1', age:22, pid: 13},
            {id: 3, name: '1-2', age:22, pid: 1},
            {id: 4, name: '1-3', age:22, pid: 1},
            {id: 5, name: '2', age:22, pid: null},
            {id: 6, name: '2-1', age:22, pid: 5},
            {id: 8, name: '2-1-1', age:22, pid: 6},
            {id: 19, name: '2-1-1-1', age:22, pid: 8},
            {id: 9, name: '2-1-2', age:22, pid: 6},
            {id: 20, name: '2-1-2-1', age:22, pid: 9},
            {id: 7, name: '2-2', age:22, pid: 5},
            {id: 17, name: '2-2-1', age:22, pid: 7},
            {id: 18, name: '2-2-2', age:22, pid: 7},
            {id: 10, name: '3', age:22, pid: null},
            {id: 15, name: '3-1', age:22, pid: 10},
            {id: 16, name: '3-2', age:22, pid: 10},
            {id: 11, name: '4', age:22, pid: 111}
		];
	    
		var gridOption3 = {
			editMode:true,
			showPager: false,
			rowRender: "tree",
			rowRenderOp: {
				parent: "pid",
				field: 'id',
				showField: 'name',
				select: 'multi',
				onlyExpandDeep: 1,
				sortData: true
			},
	        columns: [
		        {width: '150px', field: 'name', header: 'name'},
		        {width: '100px', field: 'age', header: 'age'},
		        {width: '100px', field: 'id', header: 'id', type: 'integer'},
		        {width: '100px', field: 'pid', header: 'parentId'}
	        ]
	    };
	    
	    var gridOption7 = {
			height: 400,
			showPager: false,
			editMode: true,
			customSetting: true,
			fullPanel: "#fsiStudent7",
			toolPanel: "#tbriStudent7",
			rowRender: "tree",
			rowRenderOp: {
				parent: "parentId",
				field: "id",
				showField: "name",
				select:'multi',
				loadDeep: 4,
				onlyExpandDeep: 3,
				selectParent:true,
				customIcon: {
					"def": "tree-def",
					"0": "tree-1",
					"1": "tree-2",
					"2": "tree-3"
				},
				sortData: true
	 		},
	        columns: [
				{width: 200, field: 'name', header: 'name', rule: {required: true}},
		        {width: 150, field: 'url', header: 'url'},
		        {width: 100, field: 'id', header: 'id', editable: false},
		        {width: 100, field: 'parentId', header: 'parentID', type: 'integer', fnEndEdit: function (value, rowData, td, col) {
		        	if (!value) {
		        		rowData.parentId = 0; 
		        	}
		        	rowData.parent && (rowData.parent.id = null);
		        }}
	        ],
	        ajax:{
	        	url:"services/ccay/demo/tree/list"
	        },
		    operation: {
		    	search: {
		    		btn: '#btnAllSearch7',
					panel: '#rowAllForm7'
		    	},
		    	del: {
		    		btn: '#btnDel7',
		    		undoBtn: '#btnUndoDel7'
		    	},
		    	insert: {
		    		btn: '#btnAdd7',
		    		defaultRow:{parentId: 0}
		    	},
		    	save: {
		    		btn: '#btnSave7',
		    		ajax: {
		    			url: "services/ccay/demo/tree/batch/false"
		    		}
		    	}
		    }
	    };
	    
	    var gridOption4 = {
			height: 300,
			showPager: false,
			editMode: true,
			rowRender: "tree",
			rowRenderOp: {
				parent: "parent.id",
				field: "id",
				showField: "name",
				select:'multi',
				loadDeep: 4,
				sortData: true,
				customIcon: function (data) {
					return data._deep ? "tree-def" : "tree-1";
				}
	 		},
	        columns: [
				{width: 200, field: 'name', header: 'name', rule: {required: true}},
		        {width: 150, field: 'url', header: 'url'},
		        {width: 100, field: 'id', header: 'id', editable: false},
		        {width: 100, field: 'parent.id', displayField: "data.parent ? data.parent.id : null", header: 'parentID', type: 'integer', fnEndEdit: function (value, rowData, td, col) {
		        	if (!value) {
		        		rowData.parent.id = 0; 
		        	}
		        	rowData.parentId = null;
		        }},
		        {width: 150, field: 'parent.name', displayField: "data.parent ? data.parent.name : null", header: 'parentName', editable: false}
	        ],
	        ajax:{
	        	url:"services/ccay/demo/tree/list"
	        },
		    operation: {
		    	search: {
		    		btn: '#btnAllSearch',
					panel: '#rowAllForm'
		    	},
		    	del: {
		    		btn: '#btnDel4',
		    		undoBtn: '#btnUndoDel4'
		    	},
		    	insert: {
		    		btn: '#btnAdd4',
		    		copy: true,
		    		clear: ['id'],
		    		defaultRow:{parent: {id: "0", name: ""}}
		    	},
		    	save: {
		    		btn: '#btnSave4',
		    		ajax: {
		    			url: "services/ccay/demo/tree/batch/false"
		    		}
		    	}
		    }
	    };
	    
	    var datas5 = [
			{id:1 ,name:'1111', age:22, pid:null},
			{id:6, name:'6666', age:22, pid:null},
			{id:3, name:'3333', age:22, pid:null},
			{id:5, name:'5555', age:22, pid:null},
			{id:2, name:'2222', age:22, pid:null},
			{id:4, name:'4444', age:22, pid:null},
			{id:7, name:'7777', age:22, pid:null}
		];
		
		var gridOption5 = {
			editMode: true,
			showPager: false,
			showSeq: true,
			rowRender: "tree",
			rowRenderOp: {
				parent: "pid",
				field: 'id',
				showField: 'name',
				select:'single',
				lazyLoad: true,
				loadDeep: 2,
				ajax: {getData: page.getChild}
	 		},
	        columns: [
		        {width: '150px', field: 'name', header: 'name'},
		        {width: '100px', field: 'age', header: 'age', type: 'integer'},
		        {width: '100px', field: 'id', header: 'id', type: 'integer', editable: false},
		        {width: '100px', field: 'pid', header: 'parentID', type: 'integer'}
	        ],
	        operation:{
	        	insert:{
		    		defaultRow:{id:null, name:'node', age:22, pid:null}
		    	},
		    	del:{}
	        }
	    }
	    
	    var gridOption8 = {
			height: 300,
			editMode: true,
			rowRender: "tree",
			rowRenderOp: {
				parent: "parentId",
				field: 'id',
				showField: 'name',
				select:'multi',
				loadDeep: 5,
				lazyLoad: true,
				delRoot: true,
				allowSearch: true,
				ajax: {
					url: "services/ccay/demo/tree/rootlist?parentId={0}"
				},
				customIcon: {
					"def": "tree-def",
					"1": "tree-1",
					"2": "tree-2"
				},
				fnCheckChild: function (node) {
					return node.hasChild;
				}
	 		},
	        columns: [
				{width: 200, field: 'name', header: 'name', rule: {required: true}},
		        {width: 150, field: 'url', header: 'url'},
		        {width: 100, field: 'id', header: 'id', editable: false},
		        {width: 100, field: 'parentId', header: 'parentID', type: 'integer', fnEndEdit: function (value, rowData, td, col) {
		        	if (!value) {
		        		rowData.parentId = 0; 
		        	}
		        	rowData.parent && (rowData.parent.id = null);
		        }}
	        ],
	        ajax:{
	        	url:"services/ccay/demo/tree/rootlist?parentId=0"
	        },
		    operation: {
		    	search: {
		    		btn: '#btnTreeSearch8',
					panel: '#rowTreeForm8'
		    	},
		    	del: {
		    		btn: '#btnDel8',
		    		undoBtn: '#btnUndoDel8'
		    	},
		    	insert: {
		    		btn: '#btnAdd8',
		    		copy: true,
		    		clear: ['id'],
		    		defaultRow:{parentId: 0}
		    	},
		    	save: {
		    		btn: '#btnSave8',
		    		ajax: {
		    			url: "services/ccay/demo/tree/batch/true"
		    		}
		    	}
		    }
	    };
	    
	    var gridOption6 = {
			height: 300,
			editMode: true,
			rowRender: "tree",
			rowRenderOp: {
				parent: "parent.id",
				field: 'id',
				showField: 'name',
				select:'multi',
				loadDeep: 5,
				lazyLoad: true,
				delRoot: true,
				ajax: {
					url: "services/ccay/demo/tree/rootlist?parentId={0}"
				},
				fnCheckChild:function(node){
					return node.hasChild;
				}
	 		},
	        columns: [
				{width: 200, field: 'name', header: 'name', rule: {required: true}},
		        {width: 150, field: 'url', header: 'url'},
		        {width: 100, field: 'id', header: 'id', editable: false},
		        {width: 100, field: 'parent.id', header: 'parentID', type: 'integer', fnEndEdit: function (value, rowData, td, col) {
		        	if (!value) {
		        		rowData.parent.id = 0; 
		        	}
		        	rowData.parentId = null;
		        }},
		        {width: 150, field: 'parent.name', header: 'parentName', editable: false}
	        ],
	        ajax:{
	        	url:"services/ccay/demo/tree/rootlist?parentId=0"
	        },
		    operation: {
		    	search: {
		    		btn: '#btnTreeSearch',
					panel: '#rowTreeForm'
		    	},
		    	del: {
		    		btn: '#btnDel6',
		    		undoBtn: '#btnUndoDel6'
		    	},
		    	insert: {
		    		btn: '#btnAdd6',
		    		copy: true,
		    		clear: ['id'],
		    		defaultRow:{parent: {id: "0", name: ""}}
		    	},
		    	save: {
		    		btn: '#btnSave6',
		    		ajax: {
		    			url: "services/ccay/demo/tree/batch/true"
		    		}
		    	}
		    }
	    };
		
		$S("#rowRenderDemo").ccayTab();
		
		//点击tab头后，才加载数据，只第一次生效
		$S("li[tabid='tab2']").one("click", function () {
			$S("#tbriStudent1").ccayToolbar(page.getToolOption("#iStudent1", 1));
			$S("#iStudent1").igrid(gridOption1);
		});
		
		$S("li[tabid='tab3']").one("click", function () {
			$S("#tbriStudent2").ccayToolbar(page.getToolOption("#iStudent2", 2));
			$S("#iStudent2").igrid(gridOption2);
		});
		
		$S("#hd1").one("click", function () {
			$S('#iStudent3').igrid(gridOption3, datas3);
		});
		
		$S("#hd2").one("click", function () {
			$S("#tbriStudent4").ccayToolbar(page.getToolOption("#iStudent4", 4));
			$S("#iStudent4").igrid(gridOption4);
		});
		
		$S("#hd3").one("click", function () {
			$S("#iStudent5").igrid(gridOption5, datas5);
		});
		
		$S("#hd4").one("click", function () {
			$S("#tbriStudent6").ccayToolbar(page.getToolOption("#iStudent6", 6));
			$S("#iStudent6").igrid(gridOption6);
		});
		
		$S("#hd5").one("click", function () {
			var btnItems = [{
					type: "imgBtn",
					icon: "refresh",
					text: "定位到节点：",
					click: function() {
						$S("#iStudent7").manager().goToNode($("#txtiStudent7").val());
					}
				}, {
					type: "html",
					htmlContent: '&nbsp;&nbsp;<input type="text" id="txtiStudent7" style="width:50px;"/>'
				}
			];
			$S("#tbriStudent7").ccayToolbar(page.getToolOption("#iStudent7", 7, btnItems));
			$S("#iStudent7").igrid(gridOption7);
		});
		
		$S("#hd6").one("click", function () {
			var btnItems = [{
					type: "imgBtn",
					icon: "refresh",
					text: "重新加载子节点",
					click: function() {
						$S("#iStudent8").manager().reloadChildNodes($("#txtiStudent8").val());
					}
				}, {
					type: "html",
					htmlContent: '&nbsp;&nbsp;<input type="text" id="txtiStudent8" style="width:50px;"/>'
				}
			];
			$S("#tbriStudent8").ccayToolbar(page.getToolOption("#iStudent8", 8, btnItems));
			$S("#iStudent8").igrid(gridOption8);
		});
	};
	
	page.getToolOption = function (gridId, btnIdx, btnItems) {
		var itemsOp = [{
			type: "imgBtn", 
			icon: "add",
			id: "btnAdd" + btnIdx,
			text: "ccay.common.button.gridAdd"
		}, {
			type: "menuButton", 
			defaultText : "<a class='ccay-icon del'></a><span>" + $i18n("ccay.workflow.label.delete") + "</span>",
			menus: [{
				icon: "pre-del", 
				id: "btnDel" + btnIdx,
				text: "ccay.common.button.gridDelete"
			 },{
				icon: "undel",
				id: "btnUndoDel" + btnIdx,
				text: "ccay.common.button.gridUndoDelete"
			}]
		}, {
			type: "imgBtn", 
			icon: "save",
			id: "btnSave" + btnIdx,
			text: "ccay.common.button.gridSave"
		}, {
			type: "menuButton", 
			defaultText : "<a class='ccay-icon view'></a><span>get records</span>",
			menus: [{
				icon: "status-draft",
				text: "get selected records",
				click : function() {
					page.operateTest($(gridId).manager().getSelectedRecords());
				}
			 }, {
				icon: "status-draft",
				text: "get inserted records",
				click : function() {
					page.operateTest($(gridId).manager().getInsertedRecords());
				}
			}, {
				icon: "status-draft",
				text: "get deleted records",
				click : function() {
					page.operateTest($(gridId).manager().getDeletedRecords());
				}
			}, {
				icon: "status-draft",
				text: "get updated records",
				click : function() {
					page.operateTest($(gridId).manager().getUpdatedRecords());
				}
			}]
		}];
		btnItems && (itemsOp = itemsOp.concat(btnItems));
		return {items: itemsOp};
	};
	
	page.getChild = function(success){
		var result = [
			{id:111,name:'11-1',age:22,pid:11},
			{id:113,name:'11-3',age:22,pid:11},
			{id:114,name:'11-4',age:22,pid:11},
			{id:112,name:'11-2',age:22,pid:11}
		];
		 success(result);
	 };
	
	page.operateTest = function (value) {
		alert(Ccay.DataHelper.toJsonStr(value));
	};
	
	page.openDemo = function(tabId){
		$S("#rowRenderDemo").getCcayTabManager().selectItem(tabId);
		$S("li[tabid='" + tabId + "']").click();
	};
});